package shopBean.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;

public class RestResultGenerator {
	private static final Logger LOGGER = LoggerFactory.getLogger(RestResultGenerator.class);

    /**
     * normal
     * @param success
     * @param data
     * @param message
     * @param <T>
     * @return
     */
    public static <T> RestResult<T> genResult(boolean success,String statusCode, T data, String message,String errorId) {
        RestResult<T> result = RestResult.newInstance();
        result.setSuccess(success);
        result.setData(data);
        result.setErrorId(errorId);
        result.setMsg(message);
        result.setStatusCode(statusCode);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("generate rest result:{}", result);
        }
        return result;
    }

    /**
     * success
     * @param data
     * @param <T>
     * @return
     */
    public static <T> RestResult<T> genSuccessResult(T data) {

        return genResult(true,Status.sys_success, data, "操作成功",null);
    }

    /**
     * error message
     * @param message error message
     * @param <T>
     * @return
     */
    public static <T> RestResult<T> genErrorResult(String message,String errorId,String statusCode) {

        return genResult(false,statusCode, null, message,errorId);
    }

   

    /**
     * success no message
     * @return
     */
    public static RestResult genSuccessResult() {
        return genSuccessResult(null);
    }
    public static <T> RestResult<T> genError(String message) {

        return genResult(false,Status.sys_error, null, message,null);
    }

}
